package com.cyw.common.utils;

import org.springframework.http.MediaType;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.nio.charset.StandardCharsets;

/**
 * @Author:cyw
 * @CreateTime: 2021/3/11 13:55
 **/
public class ResponseUtils {
    public static void responseJson(HttpServletResponse response, String content) throws IOException {
        responseJson(response, content, HttpServletResponse.SC_OK);
    }

    public static void responseJson(HttpServletResponse response, String content, int status) throws IOException {
        response.setContentType(MediaType.APPLICATION_JSON_VALUE);
        response.setStatus(status);
        byte[] bytes = content.getBytes(StandardCharsets.UTF_8);
        response.setContentLength(bytes.length);
        response.getOutputStream().write(bytes);
        response.flushBuffer();
    }
}